package com.zzw.mis.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>项目名称: zzw-tech </p>
 * <p>包名称: com.zzw.mis.config </p>
 * <p>描述:  </p>
 * <p>  </p>
 * <p>创建时间: 2023/1/4 17 </p>
 *
 * @author coco
 * @version v1.0
 */
@Configuration
public class RestTemplateConfig {
	@Bean("restTemplate")
	public RestTemplate restTemplate () {
		RestTemplateBuilder builder = new RestTemplateBuilder();
		List<HttpMessageConverter<?>> messageConverters = builder.build().getMessageConverters();
		for (HttpMessageConverter<?> converter : messageConverters) {
			if (converter instanceof MappingJackson2HttpMessageConverter) {
				try {
					//先将原先支持的MediaType列表拷出
					List<MediaType> mediaTypeList = new ArrayList<>(converter.getSupportedMediaTypes());
					//加入支持
					mediaTypeList.add(MediaType.TEXT_EVENT_STREAM);
					//将已经加入了stream的MediaType支持列表设置为其支持的媒体类型列表
					((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(mediaTypeList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return builder.build();
	}
}
